package com.rdc.qingkou.adapter;

import android.content.Context;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.rdc.qingkou.R;
import com.rdc.qingkou.entity.MessageBean;

import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;


public class MessageRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    private List<MessageBean> mMessageList;
    private OnItemClickListener mOnItemClickListener;

    public MessageRecyclerViewAdapter(Context context, List<MessageBean> list) {
        mContext = context;
        mMessageList = list;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_message_recyclerview, parent, false);
        return new MessageViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        final MessageViewHolder messageViewHolder = (MessageViewHolder) holder;
        messageViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mOnItemClickListener.onClick(position);
            }
        });
        messageViewHolder.timeTextView.setText(mMessageList.get(position).getTime());
        messageViewHolder.infromTextView.setText(mMessageList.get(position).getInfrom());
        if (mMessageList.get(position).getName() != null){
            messageViewHolder.nameTextView.setText(mMessageList.get(position).getName().replace("\"",""));
        }
        messageViewHolder.contentTextView.setText(mMessageList.get(position).getContent());
        Uri uri = Uri.parse("" + mMessageList.get(position).getHeadSculptureId());
        //messageViewHolder.circleImageView.setImageURI(uri);

        Glide.with(holder.itemView.getContext()).load(uri).placeholder(R.drawable.imageview_default).dontAnimate().into(messageViewHolder.circleImageView);

    }

    @Override
    public int getItemCount() {
        return mMessageList.size();  //Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
    }

    private static class MessageViewHolder extends RecyclerView.ViewHolder {

        private TextView nameTextView;
        private TextView infromTextView;
        private TextView contentTextView;
//        private SimpleDraweeView headSculptureSimpleDraweeView;
        private CircleImageView circleImageView;
        private TextView timeTextView;
        View messageView;

        public MessageViewHolder(View itemView) {
            super(itemView);
            messageView = itemView;
            nameTextView = (TextView) itemView.findViewById(R.id.item_message_name_textView);
            infromTextView = (TextView) itemView.findViewById(R.id.item_message_inform_textView);
            contentTextView = (TextView) itemView.findViewById(R.id.item_message_content_textView);
//            headSculptureSimpleDraweeView = (SimpleDraweeView) itemView.findViewById(R.id.item_message_headSculpture_simpledraweeView);
            circleImageView = (CircleImageView) itemView.findViewById(R.id.item_message_headSculpture_simpledraweeView);
            timeTextView = (TextView) itemView.findViewById(R.id.item_message_time_textView);
        }
    }

    public interface OnItemClickListener {
        void onClick(int position);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }
}
